if(length(tryCatch(list.dirs(path="~/Library/CloudStorage/Dropbox/Robots and Trade in Services"),error=function(e) e))==0){
  base.path<-"~/Dropbox/Robots and Trade in Services/"
} else {
  base.path<-"~/Library/CloudStorage/Dropbox/Robots and Trade in Services/"
}

#Load data and functions
source(paste0(base.path,"Drafts/Replication/06 Functions and Processing -- Exp 2.R"))


###
# Figure 9
###

#```{r balance tests on follow up, out.width="65%", fig.cap=paste0("Balance across foreign/domestic.  The Bowers and Hansen (2008) omnibus test p values is ",round(bh.obj.fd$overall["unstrat","p.value"],2),".")}
#library(ggeasy)

balance_controls_vec_followup<-c("educ_nodegree","educ_ba","educ_advdegree","male","hispanicbin","region","white","black","hispanicbin","hhi.num","party_dem","party_rep")

#Package RItools is produced by Bowers and Hansen
bh.obj.fd<-RItools::xBalance(
  fmla=as.formula(paste0("tmt.foreign~",paste(balance_controls_vec_followup,collapse=" + "))),
  #strata = list(party=~political_party_str,nationalism=~nationalism_questions_agreed),
  na.rm=TRUE,
  data=df.raw,
  report = c("std.diffs","p.values","chisquare.test"))

#attr(df.raw$educ_nodegree, "label") <- "Educ. - No degree"
#Hmisc::label(df.raw$educ_nodegree) <- "Educ. - No degree"

label_vec2<-c(
  "educ_nodegree"="Education (No degree)",
  "educ_ba"="Education (BA)",
  "educ_advdegree"="Education (Adv. Degree)",
  "male"="Male",
  "hispanicbin"="Ethnicity",
  "region2"="Midwest",
  "region3"="South",
  "region4"="West",
  "white"="White",
  "black"="Black",
  "hhi.num"="Household Income",
  "party_dem"="Democrat",
  "party_rep"="Republican"
)

bh.obj.fd$results %>% 
  data.frame() %>% 
  tibble::rownames_to_column() %>% 
  rename(std.diff=std.diff.unstrat,p=p.unstrat) %>% 
  ggplot(aes(std.diff,rowname,color=interaction(p<0.05,p<0.1)))+
  geom_vline(xintercept=0,linetype="dashed",color="gray25")+
  geom_point()+
  #  ggeasy::easy_labs() +
  scale_color_viridis_d(labels=c("p>0.10","p<0.1","p<0.05"))+
  scale_y_discrete(labels=label_vec2)+
  theme_bw()+
  theme(legend.position="bottom",axis.text.y=element_text(size=5))+
  labs(x="Standardized Difference",
       y="Variable",
       color="Significance",
       title="Treatment (Foreign or Domestic)")

#```





###
# Table 9
###

controls_vec<-c("educ_nodegree","educ_ba","male","hispanicbin","region","white","black","hispanicbin","hhi.num","party_dem","party_rep")

# Stargazer has to be "tricked"
#https://declaredesign.org/r/estimatr/articles/regression-tables.html

# Foreign treatment raises the regulations-benefits difference
m1 <- df.raw %>% lm(diff.btw23~tmt.foreign, .)
#   With controls
m2 <- df.raw %>% lm(paste0("diff.btw23~tmt.foreign +",paste(controls_vec,collapse=" + ")), .)
#   With controlling for step 1 responses
m3 <- df.raw %>% lm(diff.btw23~tmt.foreign + benefits1 + regs1, .)
#   With controlling for step 1 responses and controls
m4 <- df.raw %>% lm(paste0("diff.btw23~tmt.foreign + benefits1 + regs1 +",paste(controls_vec,collapse=" + ")), .)

stargazer(m1, m2, m3, m4, se = starprep(m1,m2,m3,m4),
          keep.stat="n", header = FALSE,
          title="Effect of Foreign Treatment on Difference (Regul. - Transfers), Between-respondent estimates",
          label = "tab:fubetween1",
          omit.table.layout ="dl",
          keep = c("tmt.foreign","benefits1","regs1"),
          covariate.labels=c("Foreign","Initial Trans.","Initial Regs."),
          add.lines=list(c("Controls?", "N", "Y","N","Y")), no.space=TRUE)



###
# Table 10
###

# Foreign treatment flavors raise the regulations-benefits difference
m1 <- df.raw %>% lm(diff.btw23~treatment, .)
#   With controls
m2 <- df.raw %>% lm(paste0("diff.btw23~treatment +",paste(controls_vec,collapse=" + ")), .)
#   With controlling for step 1 responses
m3 <- df.raw %>% lm(diff.btw23~treatment + benefits1 + regs1, .)
#   With controlling for step 1 responses and controls
m4 <- df.raw %>% lm(paste0("diff.btw23~treatment + benefits1 + regs1 +",paste(controls_vec,collapse=" + ")), .)

stargazer(m1, m2, m3, m4, se = starprep(m1,m2,m3,m4),
          keep.stat="n", header = FALSE,
          title="Effect of Specific Foreign Treatments on Difference (Regul. - Transfers), Between-respondent estimates",
          label = "tab:fubetween2",
          omit.table.layout ="dl",
          keep = c("treatment","benefits1","regs1","Constant"),
          covariate.labels=c("For. - Reliance","For. - Rel. Gains","For. - Within","Initial Trans.","Initial Regs."),
          add.lines=list(c("Controls?", "N", "Y","N","Y")), no.space=TRUE)


###
# Table 11
###

#```{r within respondent regressions different flavors, results='asis'}

# Reliance/Relative are significant _compared to within, using within-subject comaprisons, DiD-style
df.raw$treatment_base4 <- factor(df.raw$treatment, ordered = FALSE)
df.raw$treatment_base4 <- relevel(df.raw$treatment_base4, ref = 4)

#   No controls
m1 <- df.raw %>% lm(difference3~treatment_base4 + regs2 + benefits2, .)
#   With controls
m2 <- df.raw %>% lm(paste0("difference3~treatment_base4 + regs2 + benefits2 +",paste(controls_vec,collapse=" + ")), .)

stargazer(m1, m2, se = starprep(m1,m2),
          keep.stat="n", header = FALSE,
          title="Effect of Specific Foreign Treatments on Difference (Regul. - Transfers), Within-respondent estimates",
          label = "tab:fuwithin",
          omit.table.layout ="dl",
          keep = c("treatment","regs2","benefits2","Constant"),
          covariate.labels=c("For. - Reliance","For. - Rel. Gains","Prior Regs.","Prior Trans."),
          add.lines=list(c("Controls?", "N", "Y")), no.space=TRUE)


###
# Table 14
###
#```{r between respondent regressions whites regs, results='asis'}
#library(estimatr)
#library(stargazer)

# Regs, step 2 for domestic, step 3 for foreign; for between subject comparisons
df.raw$regs.btw23 <- ifelse(df.raw$treatment == 1, df.raw$regs2, df.raw$regs3)

controls_vec<-c("educ_nodegree","educ_ba","male","hispanicbin","region","white","black","hispanicbin","hhi.num","party_dem","party_rep")

# Stargazer has to be "tricked"
#https://declaredesign.org/r/estimatr/articles/regression-tables.html

# Foreign treatment raises the regulations-benefits difference
m1 <- df.raw %>% lm(regs.btw23~tmt.foreign*white, .)
#   With controls
m2 <- df.raw  %>% lm(paste0("regs.btw23~tmt.foreign*white +",paste(controls_vec,collapse=" + ")), .)
#   With controlling for step 1 responses
#    m3 <- df.raw  %>% lm(regs.btw23~tmt.foreign*white + benefits1 + regs1, .)
#   With controlling for step 1 responses and controls
#    m4 <- df.raw  %>% lm(paste0("regs.btw23~tmt.foreign*white + benefits1 + regs1 +",paste(controls_vec,collapse=" + ")), .)

m3 <- df.raw %>% lm(diff.btw23~tmt.foreign*white, .)
m4 <- df.raw  %>% lm(paste0("diff.btw23~tmt.foreign*white +",paste(controls_vec,collapse=" + ")), .)


stargazer(m1, m2, m3, m4, se = starprep(m1,m2,m3,m4),
          keep.stat="n", header = FALSE,
          title="Effect of Foreign Treatment on Regulations and Differences, Between-respondent estimates",
          label = "tab:fubpdiff",
          omit.table.layout ="dl",
          keep = c("tmt.foreign","white","tmt.foreign:white","Constant"),
          covariate.labels=c("Foreign","White","Foreign*White"),
          column.labels=c("Reg. Auto.","Reg. Auto.","Difference","Difference"),
          add.lines=list(c("Controls?", "N", "Y","N","Y")), no.space=TRUE)



###
# Table 15
###

#```{r between respondent regressions different flavors whites regs, results='asis'}
#library(estimatr)
#library(stargazer)

# Foreign treatment flavors raise the regulations-benefits difference
m1 <- df.raw %>% lm(regs.btw23~treatment*white + white, .)
#   With controls
m2 <- df.raw %>% lm(paste0("regs.btw23~treatment*white + white +",paste(controls_vec,collapse=" + ")), .)
#   With controlling for step 1 responses
#    m3 <- df.raw %>% lm(regs.btw23~treatment*white + white + benefits1 + regs1, .)
#   With controlling for step 1 responses and controls
#    m4 <- df.raw %>% lm(paste0("regs.btw23~treatment*white + white + benefits1 + regs1 +",paste(controls_vec,collapse=" + ")), .)

m3 <- df.raw %>% lm(diff.btw23~treatment*white + white, .)
m4 <- df.raw %>% lm(paste0("diff.btw23~treatment*white + white +",paste(controls_vec,collapse=" + ")), .)

stargazer(m1, m2, m3, m4, se = starprep(m1,m2,m3,m4),
          keep.stat="n", header = FALSE,
          title="Effect of Specific Foreign Treatments on Regs, Between-respondent estimates, white respondents",
          label = "tab:fubpdiffflavors",
          omit.table.layout ="dl",
          keep = c("treatment2","treatment3","treatment4","white","treatment2:white","treatment3:white","treatment4:white","Constant"),
          covariate.labels=c("For. - Reliance","For. - Rel. Gains","For. - Within","White","Reliance*White","Rel. Gains*White","Within*White"),
          column.labels=c("Reg. Auto.","Reg. Auto.","Difference","Difference"),
          add.lines=list(c("Controls?", "N", "Y","N","Y")), no.space=TRUE)

